﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DTO;
using DAO;

namespace BUS
{
    public class TourBUS
    {
        public List<TourDTO> layDanhSachTourChoTrangChu()
        {
            int tourNumber = 10;
            try
            {
                //CHECK HERE thieu co che lay link dong
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Công ty du lịch");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[0].MaNCC);
                float heSoGia = nccLst[0].HeSoGia;
                TourDAO dao = new TourDAO();
                List<TourDTO> tourLst = dao.layDanhSachTour(ws.DiaChiWS, ws.Username, ws.Password, "", "", "");
                if (tourLst.Count > tourNumber)
                {
                    List<TourDTO> tourLstNew = new List<TourDTO>();
                    for (int i = 0; i < tourNumber; i++)
                    {
                        tourLst[i].Price = (int)(tourLst[i].Price * heSoGia);
                        tourLstNew.Add(tourLst[i]);
                    }
                    return tourLstNew;
                }
                else
                {
                    TourDTO tour = new TourDTO();
                    tour.TourName = "Hiện chưa có thông tin";
                    tour.Price = 0;
                    tour.Description = "...";
                    for (int i = 0; i < tourLst.Count; i++)
                    {
                        tourLst[i].Price = (int)(tourLst[i].Price * heSoGia);
                    }
                    for (int i = tourLst.Count; i < tourNumber; i++)
                    {
                        tourLst.Add(tour);
                    }
                    return tourLst;
                }
            }
            catch (Exception ex)
            {
                TourDTO tour = new TourDTO();
                tour.TourName = "Hiện chưa có thông tin";
                tour.Price = 0;
                tour.Description = "...";
                List<TourDTO> tourLst = new List<TourDTO>();
                for (int i = 0; i < tourNumber; i++)
                {
                    tourLst.Add(tour);
                }
                return tourLst;
            }
        }

        public List<TourDTO> layDanhSachTour(string tourType, string departurePosition, string visitLocation)
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Công ty du lịch");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[0].MaNCC);
                TourDAO dao = new TourDAO();
                float heSoGia = nccLst[0].HeSoGia;
                List<TourDTO> tourLst = dao.layDanhSachTour(ws.DiaChiWS, ws.Username, ws.Password, tourType, departurePosition, visitLocation);

                List<TourDTO> tourLstNew = new List<TourDTO>();
                for (int i = 0; i < tourLst.Count; i++)
                {
                    tourLst[i].Price = (int)(tourLst[i].Price * heSoGia);
                    tourLstNew.Add(tourLst[i]);
                }
                return tourLstNew;

            }
            catch (Exception ex)
            {
                return new List<TourDTO>();
            }
        }

        public ListTourDetailDTO layThongTinChiTietTour(string wsId, string tripId)
        {
            //get link cua nha cung cap tuong ung voi ma
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Công ty du lịch");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[0].MaNCC);
                TourDAO dao = new TourDAO();
                float heSoGia = nccLst[0].HeSoGia;
                ListTourDetailDTO tour = dao.layThongTinChiTietTour(ws.DiaChiWS, ws.Username, ws.Password, tripId);
                for (int i = 0; i < tour.Ticket.Count; i++)
                {
                    tour.Ticket[i].Adult = (int)(tour.Ticket[i].Adult * heSoGia);
                    tour.Ticket[i].Child = (int)(tour.Ticket[i].Child * heSoGia);
                }
                return tour;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<TourDTO> timKiemTourMoRong(string tourtype, string location, float fromPrice, float toPrice, DateTime fromDate, DateTime toDate)
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Công ty du lịch");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[0].MaNCC);
                TourDAO dao = new TourDAO();
                float heSoGia = nccLst[0].HeSoGia;
                List<TourDTO> tourLst = dao.timKiemTourMoRong(ws.DiaChiWS, ws.Username, ws.Password, tourtype, location, fromPrice, toPrice, fromDate, toDate);
                for (int i = 0; i < tourLst.Count; i++)
                {
                    tourLst[i].Price = (int)(tourLst[i].Price * heSoGia);
                }
                return tourLst;
            }
            catch (Exception ex)
            {
                return new List<TourDTO>();
            }
        }

        public List<LocationDTO> layDiaDiem(string wsID)
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Công ty du lịch");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[0].MaNCC);
                TourDAO dao = new TourDAO();
                return dao.layDiaDiem(ws.DiaChiWS, ws.Username, ws.Password);
            }
            catch (Exception ex)
            {
                return new List<LocationDTO>();
            }
        }

        /// <summary>
        /// Lay thong tin nha cung cap va link webservice do
        /// </summary>
        /// <param name="loaincc"></param>
        /// <returns>NhaCungCapDTO va WebserviceDTO</returns>
        public ArrayList layThongTinNhaCungCapVaLinkWS(string loaincc)
        {
            ArrayList result = new ArrayList();
            return null;
        }
    }
}
